class Comment < ActiveRecord::Base
  
  belongs_to :user
  belongs_to :event
  
  validates_presence_of :event_id
  
  after_create :set_sender
  
  def avatar(size)
    if self.user.nil?
      case size
        when :thumb
          '/images/default_avatar_thumb.gif'
        when :mini
          '/images/default_avatar_mini.gif'
        else
          '/images/default_avatar.gif'
      end
    else
      self.user.get_avatar(size)
    end
  end
  
  private
  
  
  def set_sender
    u = User.find_by_mobile(:first, self.sender.sub("34", ""))
    self.user_id = u.id unless u.blank? or u.size > 1
    self.save
  end
  
end
